package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.Follow;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.repositories.FollowRepository;
import cn.thoughtworks.school.programCenter.services.AmazonClientService;
import cn.thoughtworks.school.programCenter.services.QuizCenterService;
import cn.thoughtworks.school.programCenter.services.ReviewQuizService;
import cn.thoughtworks.school.programCenter.services.SimpleCodingQuizService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/api")
public class SimpleCodingQuizController {

    @Autowired
    private QuizCenterService quizCenterService;
    @Autowired
    private FollowRepository followRepository;
    @Autowired
    private ReviewQuizService reviewQuizService;
    @Autowired
    private SimpleCodingQuizService simpleCodingQuizService;

    @PostMapping("simpleCodingQuizzes/submission")
    public ResponseEntity submitHomeworkQuiz(@RequestBody Map map, @Auth User user) throws BusinessException {
        map.put("studentId", user.getId());

        reviewQuizService.saveReviewQuiz(map);
        return quizCenterService.createSimpleCodingSubmission(map);
    }

    @GetMapping("simpleCodingQuizzes/users/{userId}/assignments/{assignmentId}/quizzes/{quizId}/submissions")
    public ResponseEntity getHomeworkRunningLog(@PathVariable Long assignmentId,
                                                @PathVariable Long userId,
                                                @PathVariable Long quizId) {
        Map result = quizCenterService.getSubmissionByAssignmentIdAndQuizId(userId, assignmentId, quizId);

        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    @GetMapping(value = "programs/{programId}/tutors/{tutorId}/assignments/{assignmentId}/quizzes/{quizId}/answerFile")
    public ResponseEntity getStudentsAnswerFile(@PathVariable Long quizId,
                                      @PathVariable Long assignmentId,
                                      @PathVariable Long programId,
                                      @PathVariable Long tutorId,
                                      HttpServletResponse response) throws IOException {
        String studentIds = followRepository.findByProgramIdAndTutorId(programId, tutorId)
                .stream().map(Follow::getStudentId).map(String::valueOf).collect(Collectors.joining(","));
        InputStream inputStream = quizCenterService.getStudentsAnswerFile(assignmentId, quizId, studentIds);
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();

        return ResponseEntity.ok().build();
    }

    @PostMapping(value = "simpleCodingQuizzes/users/answerFile")
    public ResponseEntity uploadAnswerFile(@RequestParam(value = "file", required = false) MultipartFile studentAnswer){
        String uploadFilePath = simpleCodingQuizService.uploadFile(studentAnswer);

        return new ResponseEntity(uploadFilePath,HttpStatus.OK);
    }
}